﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Service.Command;
using Customer.Model.Interface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveListProductServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListProductRequest, RetrieveListProductResponse>
    {
        #region Properties
        public string Dao;
        #endregion
        public RetrieveListProductServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListProductRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Type))
            {
                throw new ArgumentException("Type of product must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Group))
            {
                throw new ArgumentException("Group of type product must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.Dao;
            }
        }

        protected override bool OnExecute(RetrieveListProductResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<ListProductRecord> records = new List<ListProductRecord>();

            switch (Request.Type)
            {
                case "deposit":
                    List<string> listProductItem = Providers.Customers.RetrieveListProductDeposit(Dao, Request.Group);
                    records = listProductItem.Select(x => new ListProductRecord { ProductDeposit = x }).ToList();
                    break;
                case "loan":
                    List<string> listProductLoanItem = Providers.Customers.RetrieveListProductLoan(Dao, Request.Group);
                    records = listProductLoanItem.Select(x => new ListProductRecord { ProductLoan = x }).ToList();
                    break;
                case "card":
                    break;
                default:
                    break;
            }


            response.Records = records;
            return isSuccess;
        }


    }
}
